import { Component, OnInit } from '@angular/core';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { HttpClient } from '@angular/common/http';
import { ResultList } from '../result-list';
import { RequestMapping } from '../request.mapping';
import { AddJobComponent } from '../add-job/add-job.component';

@Component({
  selector: 'app-jobs-manager',
  templateUrl: './jobs-manager.component.html',
  styleUrls: ['./jobs-manager.component.css']
})
export class JobsManagerComponent implements OnInit {
  isCollapsed = false;
  page: any;

  _dataSet = [];
  _allChecked = false;
  _indeterminate = false;
  constructor(public modalService: NzModalService, public http: HttpClient, public messageService: NzMessageService) { }

  ngOnInit() {
    this.loadData();
  }

  loadData() {
    this.http.get<ResultList>(RequestMapping.JOBS).subscribe(response => {
      this._dataSet = response.data;
    })
  }

  openAddJobModal(){
    this.modalService.open({title:"添加任务",content:AddJobComponent,maskClosable:false})
  }



  _displayDataChange($event) {
    this._dataSet = $event;
    this._refreshStatus();
  }

  _refreshStatus() {
    const allChecked = this._dataSet.every(value => value.checked === true);
    const allUnChecked = this._dataSet.every(value => !value.checked);
    this._allChecked = allChecked;
    this._indeterminate = (!allChecked) && (!allUnChecked);
  }

  _checkAll(value) {
    if (value) {
      this._dataSet.forEach(data => {
        data.checked = true;
      });
    } else {
      this._dataSet.forEach(data => {
        data.checked = false;
      });
    }
    this._refreshStatus();
  }


}
